//
//  XFSelectView.m
//  WorkManager
//
//  Created by ukongm on 17/2/9.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "XFSelectView.h"

/** 导航的高度 */
static CGFloat const NAVI_HEIGHT = 50;
/** 间隔 */
static CGFloat const VIEW_GAP = 10;
/** 动画时间 */
static CGFloat const TIME = 0.25;
@interface XFSelectView()<UITableViewDelegate,UITableViewDataSource>

/** 导航 */
@property(nonatomic,strong)UIView* naviView;
/** 遮罩 */
@property(nonatomic,strong)UIView* backView;
/** 列表视图 */
@property(nonatomic,strong)UITableView* tableView;



@end

@implementation XFSelectView
{
    CGRect _frame; //开始的位置
    
    UILabel* _titleLb; //标题
    UIButton* _fixedBtn; //确定按钮
    void (^_block)(NSInteger); //保存回调事件
    NSInteger _curIndex; //当前选中的index，不过没有确定，下次出现重置
}

- (BOOL)multis{
    return NO;
}

- (UITableView *)tableView{
    
    if (_tableView == nil) {
        CGFloat x = VIEW_GAP;
        CGFloat y = self.naviView.maxY + VIEW_GAP/2;
        CGFloat width = self.width - VIEW_GAP * 2;
        CGFloat height = self.height - self.naviView.maxY - VIEW_GAP;
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(x, y, width, height) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.allowsMultipleSelection = self.multis;
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    }
    return _tableView;
}

- (UIView *)backView{
    
    if (_backView == nil) {
        _backView = [[UIView alloc] initWithFrame:CGRectZero];
        _backView.alpha = 0;
        _backView.backgroundColor = [UIColor blackColor];
        [_backView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)]];
    }
    return _backView;
}

- (UIView *)naviView{
    
    if (_naviView == nil) {
        CGFloat gap = VIEW_GAP;
        CGFloat width = 50;
        _naviView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, NAVI_HEIGHT)];
        _naviView.backgroundColor = [UIColor red:200 green:200 blue:200];
        
        _titleLb = [[UILabel alloc] initWithFrame:_naviView.bounds];
        _titleLb.font = [UIFont systemFontOfSize:15];
        _titleLb.textColor = [UIColor red:30 green:30 blue:30];
        _titleLb.textAlignment = NSTextAlignmentCenter;
        
        UIButton* cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(gap, 0, width, _naviView.height)];
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
        
        _fixedBtn = [[UIButton alloc] initWithFrame:CGRectMake(_naviView.width - gap - width, 0, width, _naviView.height)];
        _fixedBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [_fixedBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_fixedBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_fixedBtn addTarget:self action:@selector(fixedAction) forControlEvents:UIControlEventTouchUpInside];
        _fixedBtn.enabled = NO;
        
        [_naviView addSubview:_titleLb];
        [_naviView addSubview:cancelBtn];
        [_naviView addSubview:_fixedBtn];
    }
    return _naviView;
}

- (instancetype)init
{
    CGFloat width = self.screenSize.width * 0.9;
    CGFloat height = 105;
    CGFloat x = (self.screenSize.width - width)/2;
    CGFloat y = self.screenSize.height;
    
    _frame = CGRectMake(x, y, width, height);
    
    
    return [self initWithFrame:_frame];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.naviView];
        [self addSubview:self.tableView];
        _index = -1;
        [self layerWithRadian:3];
    }
    return self;
}

- (void)hide{
    [UIView animateWithDuration:TIME animations:^{
        self.frame = CGRectMake(_frame.origin.x, _frame.origin.y, _frame.size.width, self.height);
        self.backView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.backView removeFromSuperview];
    }];
}

- (void)fixedAction{
    _index = _curIndex;
    if (_block) {
        _block(self.index);
    }
    [self hide];
}


- (CGFloat)maxHeight{
    if (_maxHeight == 0) {
        return 200;
    }
    return _maxHeight;
}

- (void)setTitle:(NSString *)title{
    _titleLb.text = title;
    _title = title;
}

- (void)setTitles:(NSArray *)titles{
    _titles = titles;
    CGFloat tableHeight = self.tableView.height;
    [self.tableView reloadData];
    //改变该控件的高度
    CGFloat scale = self.tableView.contentSize.height - tableHeight;
    CGFloat reuslt = self.height + scale;
    
    
    if (reuslt > self.maxHeight) {
        reuslt = self.maxHeight;
    }
    [self changeHeight:reuslt];
}

- (void)showInView:(UIView *)view{
    [self showInView:view fixedBlock:nil];
}

- (void)showInView:(UIView *)view fixedBlock:(void (^)(NSInteger))fixedBlock{
    self.backView.frame = view.bounds;
    [view addSubview:self.backView];
    [view addSubview:self];
    _block = fixedBlock;
    _fixedBtn.enabled = NO;
    [_fixedBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    if (self.multis == NO) {
        if (self.index >= 0 && self.index < self.titles.count) {
            [self setSelectItem:self.index];
        }
    }
    
    [UIView animateWithDuration:TIME animations:^{
       
        self.backView.alpha = 0.8;
        [self changeY:_frame.origin.y - self.height - VIEW_GAP];
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString* cellID = @"list";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = self.titles[indexPath.section];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 8;
}

- (void)fixedBtnChangeStyle{
    if (_fixedBtn.isEnabled == NO) {
        _fixedBtn.enabled = YES;
        [_fixedBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self fixedBtnChangeStyle];
    
    
    
    _curIndex = indexPath.section;
}

- (void)setSelectItem:(NSInteger)index{
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] animated:NO scrollPosition:UITableViewScrollPositionNone];
}

@end
